<?php
	class WZ_controller
	{
        static $_instance;
        public $_options;
        public $_request;
        public $_classResources;
        public $view;
        
        public static function getInstance()
        {
            if (null === self::$_instance) {
                self::$_instance = new self();
            }
            return self::$_instance;
        }
        
        public function getApp()
        {
            self::getInstance();
            $app = \WZ::get('WZApp');
            if ($app instanceof \WZ_Application) {
                return $app;
            }
            return null;
        }
        
        public function getClassResources()
        {
            if (null === $this->_classResources) {
                if (version_compare(PHP_VERSION, '5.2.6') === -1) {
                    $class        = new ReflectionObject($this);
                    $classMethods = $class->getMethods();
                    $methodNames  = array();

                    foreach ($classMethods as $method) {
                        $methodNames[] = $method->getName();
                    }
                } else {
                    $methodNames = get_class_methods($this);
                }

                $this->_classResources = array();
                foreach ($methodNames as $method) {
                    if (5 < strlen($method) && '_init' === substr($method, 0, 5)) {
                        $this->_classResources[strtolower(substr($method, 5))] = $method;
                    }
                }
            }
            return $this->_classResources;
        }
        
        public function getRequest()
        {
            return \WZ_Request::getInstance();
        }
        
        public function getBootstrap()
        {
            return \WZ_Bootstrap::getInstance();
        }
        
        public function __call($method, $args)
        {
            $methodCalled = substr($method, 0, 3);
            if ($methodCalled == 'get') {
                $var = strtolower(substr($method, 3));
                $res = \WZ::get($var);
                if (null === $res) {
                    $res = \WZ::get(substr($method, 3));
                }
                if (null === $res) {
                    $res = \WZ::get(lcfirst(substr($method, 3)));
                }
                if (null === $res) {
                    $res = \WZ_Request::getPost($var);
                }
                if (null === $res) {
                    $res = \WZ_Request::getGet($var);
                }
                if (null === $res) {
                    $res = \WZ_Request::getSession($var);
                }
                if (null === $res) {
                    $res = \WZ_Request::getCookie($var);
                }
                return $res;
            } else if ($methodCalled == 'set') {
                $var = strtolower(substr($method, 3));
                return \WZ::set($var, $args[0]);
            }
        }
	}
?>